<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="外部编号" prop="foreignId">
        <el-input
          v-model="queryParams.foreignId"
          placeholder="外部编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录名" prop="logName">
        <el-input
          v-model="queryParams.logName"
          placeholder="请输入登录名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column label="外部编号" align="center" prop="foreignId" width="350"/>
      <el-table-column label="最后修改时间" align="center" prop="lastUpdateTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="登录名" align="center" prop="logName" />
      <el-table-column label="联系电话" align="center" prop="tel" />
      <el-table-column label="手机号" align="center" prop="mobile" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赣政通用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="250px">
        <el-form-item label="外部编号,对应政务钉钉的employeeCode" prop="foreignId">
          <el-input v-model="form.foreignId" placeholder="请输入外部编号,对应政务钉钉的employeeCode" />
        </el-form-item>
        <el-form-item label="删除标识,-1已删除；&gt;=0正常" prop="rowState">
          <el-input v-model="form.rowState" placeholder="请输入删除标识,-1已删除；&gt;=0正常" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastUpdateTime">
          <el-date-picker clearable
            v-model="form.lastUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="登录名" prop="logName">
          <el-input v-model="form.logName" placeholder="请输入登录名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="工号" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAdd">
          <el-input v-model="form.homeAdd" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="家庭电话" prop="homeTel">
          <el-input v-model="form.homeTel" placeholder="请输入家庭电话" />
        </el-form-item>
        <el-form-item label="家庭邮编" prop="homeZipcode">
          <el-input v-model="form.homeZipcode" placeholder="请输入家庭邮编" />
        </el-form-item>
        <el-form-item label="传真" prop="fax">
          <el-input v-model="form.fax" placeholder="请输入传真" />
        </el-form-item>
        <el-form-item label="拼音全拼" prop="pinyin">
          <el-input v-model="form.pinyin" placeholder="请输入拼音全拼" />
        </el-form-item>
        <el-form-item label="拼音首字母" prop="pinyinInitial">
          <el-input v-model="form.pinyinInitial" placeholder="请输入拼音首字母" />
        </el-form-item>
        <el-form-item label="职级编码" prop="careerRank">
          <el-input v-model="form.careerRank" placeholder="请输入职级编码" />
        </el-form-item>
        <el-form-item label="职级" prop="careerRankText">
          <el-input v-model="form.careerRankText" placeholder="请输入职级" />
        </el-form-item>
        <el-form-item label="职务" prop="unitRank">
          <el-input v-model="form.unitRank" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="性别" prop="sexText">
          <el-input v-model="form.sexText" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="教育程度编码" prop="education">
          <el-input v-model="form.education" placeholder="请输入教育程度编码" />
        </el-form-item>
        <el-form-item label="教育程度" prop="educationText">
          <el-input v-model="form.educationText" placeholder="请输入教育程度" />
        </el-form-item>
        <el-form-item label="状态" prop="statusText">
          <el-input v-model="form.statusText" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="参加工作时间" prop="joinWork">
          <el-date-picker clearable
            v-model="form.joinWork"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择参加工作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="进入本单位时间" prop="joinCompany">
          <el-date-picker clearable
            v-model="form.joinCompany"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择进入本单位时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="显示顺序" prop="showOrder">
          <el-input v-model="form.showOrder" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="密级等级" prop="mjLevel">
          <el-input v-model="form.mjLevel" placeholder="请输入密级等级" />
        </el-form-item>
        <el-form-item label="密级等级" prop="mjLevelText">
          <el-input v-model="form.mjLevelText" placeholder="请输入密级等级" />
        </el-form-item>
        <el-form-item label="直接领导" prop="leaderText">
          <el-input v-model="form.leaderText" placeholder="请输入直接领导" />
        </el-form-item>
        <el-form-item label="直接领导ID" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入直接领导ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="头像" prop="photo">
          <el-input v-model="form.photo" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="聊天工具ID" prop="imId">
          <el-input v-model="form.imId" placeholder="请输入聊天工具ID" />
        </el-form-item>
        <el-form-item label="办公地点" prop="workPlace">
          <el-input v-model="form.workPlace" placeholder="请输入办公地点" />
        </el-form-item>
        <el-form-item label="CA证书序列号" prop="certSn">
          <el-input v-model="form.certSn" placeholder="请输入CA证书序列号" />
        </el-form-item>
        <el-form-item label="签名图片" prop="signPhoto">
          <el-input v-model="form.signPhoto" placeholder="请输入签名图片" />
        </el-form-item>
        <el-form-item label="智能柜标识" prop="cabinetCode">
          <el-input v-model="form.cabinetCode" placeholder="请输入智能柜标识" />
        </el-form-item>
        <el-form-item label="是否在编用户,-1否，1是" prop="isFormation">
          <el-input v-model="form.isFormation" placeholder="请输入是否在编用户,-1否，1是" />
        </el-form-item>
        <el-form-item label="是否在编用户" prop="isFormationText">
          <el-input v-model="form.isFormationText" placeholder="请输入是否在编用户" />
        </el-form-item>
        <el-form-item label="职称编码" prop="professionalRank">
          <el-input v-model="form.professionalRank" placeholder="请输入职称编码" />
        </el-form-item>
        <el-form-item label="职称" prop="professionalRankText">
          <el-input v-model="form.professionalRankText" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="人员编制编号" prop="formation">
          <el-input v-model="form.formation" placeholder="请输入人员编制编号" />
        </el-form-item>
        <el-form-item label="人员编制名称" prop="formationText">
          <el-input v-model="form.formationText" placeholder="请输入人员编制名称" />
        </el-form-item>
        <el-form-item label="机构ID" prop="unitId">
          <el-input v-model="form.unitId" placeholder="请输入机构ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/gzt/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赣政通用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        foreignId: null,
        rowState: null,
        lastUpdateTime: null,
        userName: null,
        logName: null,
        password: null,
        cardId: null,
        nickName: null,
        tel: null,
        mobile: null,
        email: null,
        homeAdd: null,
        homeTel: null,
        homeZipcode: null,
        fax: null,
        pinyin: null,
        pinyinInitial: null,
        careerRank: null,
        careerRankText: null,
        unitRank: null,
        sex: null,
        sexText: null,
        education: null,
        educationText: null,
        status: null,
        statusText: null,
        birthday: null,
        age: null,
        joinWork: null,
        joinCompany: null,
        showOrder: null,
        mjLevel: null,
        mjLevelText: null,
        leaderText: null,
        leader: null,
        photo: null,
        idNumber: null,
        imId: null,
        workPlace: null,
        certSn: null,
        signPhoto: null,
        cabinetCode: null,
        isFormation: null,
        isFormationText: null,
        professionalRank: null,
        professionalRankText: null,
        formation: null,
        formationText: null,
        unitId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        foreignId: [
          { required: true, message: "外部编号,对应政务钉钉的employeeCode不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询赣政通用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        foreignId: null,
        rowState: null,
        lastUpdateTime: null,
        userName: null,
        logName: null,
        password: null,
        cardId: null,
        nickName: null,
        tel: null,
        mobile: null,
        email: null,
        homeAdd: null,
        homeTel: null,
        homeZipcode: null,
        fax: null,
        pinyin: null,
        pinyinInitial: null,
        careerRank: null,
        careerRankText: null,
        unitRank: null,
        sex: null,
        sexText: null,
        education: null,
        educationText: null,
        status: null,
        statusText: null,
        birthday: null,
        age: null,
        joinWork: null,
        joinCompany: null,
        showOrder: null,
        mjLevel: null,
        mjLevelText: null,
        leaderText: null,
        leader: null,
        remark: null,
        photo: null,
        idNumber: null,
        imId: null,
        workPlace: null,
        certSn: null,
        signPhoto: null,
        cabinetCode: null,
        isFormation: null,
        isFormationText: null,
        professionalRank: null,
        professionalRankText: null,
        formation: null,
        formationText: null,
        unitId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赣政通用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赣政通用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除赣政通用户编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gzt/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
